package com.example.a_java.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.a_java.common.Result;
import com.example.a_java.dto.CustomerDTO;
import com.example.a_java.dto.CustomerQueryDTO;
import com.example.a_java.dto.CustomerStatusDTO;
import com.example.a_java.dto.CustomerVisitDTO;
import com.example.a_java.service.CustomerService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

/**
 * 客户管理Controller
 */
@Tag(name = "客户管理", description = "客户信息相关接口")
@RestController
@RequestMapping("/customer")
@RequiredArgsConstructor
@Validated
public class CustomerController {

    private final CustomerService customerService;

    /**
     * 获取客户列表
     */
    @Operation(summary = "获取客户列表")
    @GetMapping("/info/list")
    public Result<Map<String, Object>> list(CustomerQueryDTO query) {
        IPage<CustomerDTO> page = customerService.listCustomers(query);
        
        Map<String, Object> result = new HashMap<>();
        result.put("total", page.getTotal());
        result.put("rows", page.getRecords());
        
        return Result.success(result);
    }

    /**
     * 获取客户详情
     */
    @Operation(summary = "获取客户详情")
    @GetMapping("/info/{custId}")
    public Result<CustomerDTO> getInfo(@Parameter(description = "客户ID") @PathVariable Long custId) {
        return Result.success(customerService.getCustomerById(custId));
    }

    /**
     * 新增客户
     */
    @Operation(summary = "新增客户")
    @PostMapping("/info")
    public Result<Map<String, Object>> add(@RequestBody @Valid CustomerDTO customerDTO) {
        Long id = customerService.addCustomer(customerDTO);
        
        Map<String, Object> result = new HashMap<>();
        result.put("id", id);
        
        return Result.success(result);
    }

    /**
     * 修改客户信息
     */
    @Operation(summary = "修改客户信息")
    @PutMapping("/info")
    public Result<Void> update(@RequestBody @Valid CustomerDTO customerDTO) {
        customerService.updateCustomer(customerDTO);
        return Result.success();
    }

    /**
     * 删除客户
     */
    @Operation(summary = "删除客户")
    @DeleteMapping("/info/{custIds}")
    public Result<Void> delete(@Parameter(description = "客户ID，多个以逗号分隔") @PathVariable String custIds) {
        customerService.deleteCustomers(custIds);
        return Result.success();
    }

    /**
     * 更新客户状态
     */
    @Operation(summary = "更新客户状态")
    @PutMapping("/info/status")
    public Result<Void> updateStatus(@RequestBody @Valid CustomerStatusDTO statusDTO) {
        customerService.updateCustomerStatus(statusDTO);
        return Result.success();
    }

    /**
     * 获取客户回访记录列表
     */
    @Operation(summary = "获取客户回访记录列表")
    @GetMapping("/visit/list")
    public Result<Map<String, Object>> listVisitRecords(
            @Parameter(description = "客户ID") @RequestParam Long custId,
            @Parameter(description = "当前页码") @RequestParam(defaultValue = "1") Integer pageNum,
            @Parameter(description = "每页记录数") @RequestParam(defaultValue = "10") Integer pageSize) {
        
        IPage<CustomerVisitDTO> page = customerService.listVisitRecords(custId, pageNum, pageSize);
        
        Map<String, Object> result = new HashMap<>();
        result.put("total", page.getTotal());
        result.put("rows", page.getRecords());
        
        return Result.success(result);
    }

    /**
     * 添加回访记录
     */
    @Operation(summary = "添加回访记录")
    @PostMapping("/visit")
    public Result<Map<String, Object>> addVisitRecord(@RequestBody @Valid CustomerVisitDTO visitDTO) {
        Long id = customerService.addVisitRecord(visitDTO);
        
        Map<String, Object> result = new HashMap<>();
        result.put("id", id);
        
        return Result.success(result);
    }

    /**
     * 删除回访记录
     */
    @Operation(summary = "删除回访记录")
    @DeleteMapping("/visit/{visitIds}")
    public Result<Void> deleteVisitRecords(@Parameter(description = "回访记录ID，多个以逗号分隔") @PathVariable String visitIds) {
        customerService.deleteVisitRecords(visitIds);
        return Result.success();
    }

    /**
     * 获取客户贷款记录列表
     */
    @Operation(summary = "获取客户贷款记录列表")
    @GetMapping("/loan/list")
    public Result<Map<String, Object>> listCustomerLoans(
            @Parameter(description = "客户ID") @RequestParam Long custId,
            @Parameter(description = "当前页码") @RequestParam(defaultValue = "1") Integer pageNum,
            @Parameter(description = "每页记录数") @RequestParam(defaultValue = "10") Integer pageSize,
            @Parameter(description = "贷款状态") @RequestParam(required = false) String status) {
        
        IPage<Map<String, Object>> page = customerService.listCustomerLoans(custId, pageNum, pageSize, status);
        
        Map<String, Object> result = new HashMap<>();
        result.put("total", page.getTotal());
        result.put("rows", page.getRecords());
        
        return Result.success(result);
    }

    /**
     * 获取贷款详情
     */
    @Operation(summary = "获取贷款详情")
    @GetMapping("/loan/{loanId}")
    public Result<Map<String, Object>> getLoanDetail(@Parameter(description = "贷款ID") @PathVariable Long loanId) {
        return Result.success(customerService.getLoanById(loanId));
    }
}
